package cao.oschina.widget.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import cao.oschina.R;
import cao.oschina.app.AppContext;
import hd.util.ScreenHelper;

/**
 * 自定义Dialog
 * <p/>
 * Created by haidy on 14/12/23.
 */
public class BaseDialog extends android.app.Dialog {

    private View view;
    private View titleView;
    private View bodyView;
    private View actionView;
    private Bitmap bitmap;

    /**
     * Create a Dialog window that uses the default dialog frame style.
     *
     * @param context The Context the Dialog is to run it.  In particular, it
     *                uses the window manager and theme in this context to
     *                present its UI.
     */
    public BaseDialog(Context context) {
        this(context, R.style.dialog_style);
    }

    /**
     * Create a Dialog window that uses a custom dialog style.
     *
     * @param context The Context in which the Dialog should run. In particular, it
     *                uses the window manager and theme from this context to
     *                present its UI.
     * @param theme   A style resource describing the theme to use for the
     *                window. See <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style
     *                and Theme Resources</a> for more information about defining and using
     *                styles.  This theme is applied on top of the current theme in
     */
    public BaseDialog(Context context, int theme) {
        super(context, theme);
        setContentView(view = View.inflate(getContext(), R.layout.dialog_view, null));
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        int maxWidth = 320;//dp
        lp.width = ((AppContext) context.getApplicationContext()).getScreenWidth() - ScreenHelper.dp2px(getContext(), 40);
        if (ScreenHelper.px2dp(getContext(), lp.width) > maxWidth)
            lp.width = ScreenHelper.dp2px(getContext(), maxWidth);
        lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        lp.dimAmount = 0.4f;
        getWindow().setAttributes(lp);
        //添加默认视图
        setTitleView(R.layout.dialog_default_title_text_view);
        setBodyView(R.layout.dialog_default_content_text_view);
    }

    public void setActionView(int layout) {
        setActionView(View.inflate(getContext(), layout, null));
    }

    @Override
    public void setOnShowListener(OnShowListener listener) {
        super.setOnShowListener(listener);
    }

    public void setActionView(View view) {
        if (actionView != view) {
            actionView = view;
            ((ViewGroup) this.view.findViewById(R.id.dialog_action_view)).removeAllViews();
            ((ViewGroup) this.view.findViewById(R.id.dialog_action_view)).addView(view);
        }
    }

    /**
     * 设置内容布局
     *
     * @param layout 布局文件
     */
    public void setBodyView(int layout) {
        setBodyView(View.inflate(getContext(), layout, null));
    }

    /**
     * 设置内容布局
     *
     * @param view view
     */
    public void setBodyView(View view) {
        if (this.bodyView != view) {
            bodyView = view;
            ((ViewGroup) this.view.findViewById(R.id.dialog_sv)).removeAllViews();
            ((ViewGroup) this.view.findViewById(R.id.dialog_sv)).addView(view);
        }
    }

    /**
     * 设置TitleView
     *
     * @param layout 布局文件
     */
    public void setTitleView(int layout) {
        setTitleView(View.inflate(getContext(), layout, null));
    }

    /**
     * 设置TitleView
     *
     * @param titleView view
     */
    public void setTitleView(View titleView) {
        if (this.titleView != titleView) {
            this.titleView = titleView;
            ((ViewGroup) view.findViewById(R.id.title_view)).removeAllViews();
            ((ViewGroup) view.findViewById(R.id.title_view)).addView(titleView);
        }
    }

    /**
     * 获取标题视图
     *
     * @return titleView
     */
    public View getTitleView() {
        return titleView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 获取actionView
     *
     * @return actionView
     */
    public View getActionView() {
        return actionView;
    }

    /**
     * 获取ContentView
     *
     * @return bodyView
     */
    public View getBodyView() {
        return bodyView;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }
    }
}
